﻿#include "DXUT.h"
#include "DXUTImageButton.h"


#include "SDKmisc.h"

CDXUTImageButton::CDXUTImageButton( RECT rtClick,RECT rtOver,const LPTSTR pszClickTexture,const LPTSTR pszOverTexture ,CSound * pClickSound,CSound * pMouseOverSound, CDXUTDialog* pDialog )
	:CDXUTButton(pDialog) ,m_pClickSound(pClickSound),m_pMouseOverSound(pMouseOverSound)
{

	m_Type = (DXUT_CONTROL_TYPE)(DXUT_CONTROL_IMAGEBUTTON);

	CDXUTElement elem;
	TCHAR szFileName [1024];
	DWORD len=1024;

	//单击纹理
	//分配一个id
	
	elem.iTexture = pDialog->m_Textures.GetSize();

	DXUTFindDXSDKMediaFileCch(szFileName,len, pszClickTexture);  ;
	pDialog->SetTexture(elem.iTexture ,szFileName) ;
	elem.iFont = 0;





	elem.SetTexture(elem.iTexture, &rtClick, D3DCOLOR_ARGB(255, 255, 255, 255));
	pDialog->SetDefaultElement(DXUT_CONTROL_IMAGEBUTTON, 0, &elem);

	//over纹理
	//分配一个id
	elem.iTexture = pDialog->m_Textures.GetSize	();

	DXUTFindDXSDKMediaFileCch(szFileName,len, pszOverTexture);  ;
	pDialog->SetTexture(elem.iTexture ,szFileName) ;
	elem.SetTexture(elem.iTexture, &rtOver, D3DCOLOR_ARGB(255, 255, 255, 255));
	pDialog->SetDefaultElement(DXUT_CONTROL_IMAGEBUTTON, 1, &elem);




}


CDXUTImageButton::~CDXUTImageButton(void)
{
}

void CDXUTImageButton::Render( float fElapsedTime )
{
	int TextureIndex=0;
	int nOffsetX = 0;
	int nOffsetY = 0;

	DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;

	if( m_bVisible == false )
	{
		iState = DXUT_STATE_HIDDEN;
	}
	else if( m_bEnabled == false )
	{
		iState = DXUT_STATE_DISABLED;
	}
	else if( m_bPressed )
	{
		if(m_pClickSound)
			m_pClickSound->Play();
		iState = DXUT_STATE_PRESSED;

		nOffsetX = 1;
		nOffsetY = 2;
	}
	else if( m_bMouseOver )
	{
		if(m_pMouseOverSound)
			m_pMouseOverSound->Play();
		iState = DXUT_STATE_MOUSEOVER;
		TextureIndex=1;
		nOffsetX = -1;
		nOffsetY = -2;
	}
	else if( m_bHasFocus )
	{
		iState = DXUT_STATE_FOCUS;
	}

	// Background fill layer
	CDXUTElement* pElement = m_Elements.GetAt( TextureIndex );

	float fBlendRate = ( iState == DXUT_STATE_PRESSED ) ? 0.0f : 0.8f;
	pElement->TextureColor.Blend( iState, fElapsedTime, fBlendRate );

	RECT rcWindow = m_rcBoundingBox;
	OffsetRect( &rcWindow, nOffsetX, nOffsetY );

	m_pDialog->DrawSprite( pElement, &rcWindow, 0.8f );



}

void CDXUTImageButton::AddToDialog( int x, int y,int widht,int hegiht,int ID,CDXUTDialog * pDialog )
{
	pDialog->AddControl(this); 
	SetLocation(x,y);
	SetVisible(true);
	SetSize( widht, hegiht );
	SetID( ID );
}
